import { create } from "zustand"
import { immer } from "zustand/middleware/immer"
import { devtools } from "zustand/middleware"
import { DEFAULT_CONVERSATIONS_ITEMS } from "@/config/mock"
import type { Conversation } from "@ant-design/x"

type ChatState = {
  conversations: Conversation[] // 会话列表
  curConversation: string // 当前会话
  setConversationsAction: (newConversations: Conversation[]) => void // 设置会话列表
  setCurConversationAction: (newConversation: string) => void // 设置当前会话
}

const useChatStore = create<ChatState>()(
  devtools(
    immer((set) => ({
      conversations: DEFAULT_CONVERSATIONS_ITEMS,
      curConversation: DEFAULT_CONVERSATIONS_ITEMS[0].key,
      setConversationsAction: (newConversations: Conversation[]) => {
        set((state) => {
          state.conversations = newConversations
        })
      },
      setCurConversationAction: (newCurConversation: string) => {
        set((state) => {
          state.curConversation = newCurConversation
        })
      }
    }))
  )
)

export default useChatStore
